﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class DataGrid自定义分页 : System.Web.UI.Page
{
    private string connstr;
    private string sql;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.EditDataBind();
        
        }
    }
    /*
    void sjtc()
    {
        OleDbConnection lj = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("lyb.mdb"));
        OleDbDataAdapter ml = new OleDbDataAdapter("select * from 留言 order by 编号 desc", lj);
        DataSet sj = new DataSet();
        ml.Fill(sj);

        int zs = sj.Tables[0].Rows.Count; //总记录数
        int fys = int.Parse(ConfigurationSettings.AppSettings["每页记录数"]);//将数字的字符串表示形式转换为它的等效 32 位有符号整数。
        int ys = zs / fys; //总页数
        if (zs % fys != 0)
            ys++;

        PagedDataSource fy = new PagedDataSource();
        fy.DataSource = sj.Tables[0].DefaultView;
        fy.AllowPaging = true;
        fy.PageSize = fys;
        int y;
        try
        {
            y = int.Parse(Request.QueryString["y"]);
        }
        catch
        {
            y = 1;
        }
        fy.CurrentPageIndex = y - 1;
        a_xsly.DataSource = fy;
        a_xsly.DataBind();

        HyperLink s = null, x = null;
        s = (HyperLink)a_xsly.Controls[a_xsly.Controls.Count - 1].FindControl("a_sy");
        x = (HyperLink)a_xsly.Controls[a_xsly.Controls.Count - 1].FindControl("a_xy");
        if (!fy.IsFirstPage)
            s.NavigateUrl = "index.aspx?y=" + (y - 1);
        if (!fy.IsLastPage)
            x.NavigateUrl = "index.aspx?y=" + (y + 1);
        ((Label)a_xsly.Controls[a_xsly.Controls.Count - 1].FindControl("a_sltj")).Text = "共有&nbsp;<span class=\"hsz\">" + zs + "</span>&nbsp;条留言/&nbsp;<span class=\"hsz\">" + ys + "</span>&nbsp;个页面&nbsp;&nbsp;当前是第&nbsp;<span class=\"hsz\">" + y + "</span>&nbsp;页(<span class=\"hsz\">" + a_xsly.Items.Count + "</span>&nbsp;条留言)&nbsp;<a href=\"index.aspx\">首页</a>&nbsp;<a href=\"index.aspx?y=" + ys + "\">尾页</a>";
    }
*/
    //分页

    private void EditDataBind()
    {

        connstr = ConfigurationSettings.AppSettings["SqlConn"];
        sql = "Select * from zhuanti";
        SqlConnection MyConn = new SqlConnection(connstr);
        SqlDataAdapter Da = new SqlDataAdapter(sql, MyConn);
        Da.SelectCommand.CommandType = CommandType.Text;
        DataSet MDs = new DataSet();
        Da.Fill(MDs);

        Gridx.DataSource = MDs;
        Gridx.DataBind();
    
    }
    /*
    protected void PageLast_Click(object sender, EventArgs e)
    {
        //取得首页专的标志,
        string ardx = ((LinkButton)sender).CommandArgument;
        //开始选择
        switch(ardx)
        {
            case "First":
            {
                Gridx.CurrentPageIndex = 0;
                break;
            }
            case "Up"://上一页
            {
                Gridx.CurrentPageIndex = (int)Math.Max(0,Gridx.CurrentPageIndex - 1);
                break;
            } 
            case "Nextx"://下一页
            {
                Gridx.CurrentPageIndex = (int)Math.Min(Gridx.PageCount - 1, Gridx.CurrentPageIndex + 1);
                break;
            }
            case "Last"://最后一页
            {
                Gridx.CurrentPageIndex = Gridx.PageCount-1;
                break;
            }

        }
        /////

        this.EditDataBind();
        

    }
     */
    public void PageButtonClick(object sender, EventArgs e)
    {
        //取得按钮单击时传递的命令参数
        string arg = ((LinkButton)sender).CommandArgument.ToString();
        switch (arg)
        {
            //如果点击的是“下一页”
            case "next":
                //如果当前页不是最后一页
                if (Gridx.CurrentPageIndex < (Gridx.PageCount - 1))
                {
                    //设置DataGrid控件的当前页索引为下一页面
                    Gridx.CurrentPageIndex += 1;
                }
                break;
            //如果点击的是“前一页”
            case "prev":
                //如果当前页不是首页
                if (Gridx.CurrentPageIndex > 0)
                {
                    //设置DataGrid控件的当前页索引为上一页面
                    Gridx.CurrentPageIndex -= 1;
                }
                break;
            //如果点击的是“最后页”
            case "last":
                //设置当前页的索引为最后一页
                Gridx.CurrentPageIndex = (Gridx.PageCount - 1);
                break;
            //默认为”最首页“
            default:
                //设置当前页的索引为首页
                Gridx.CurrentPageIndex = System.Convert.ToInt32(arg);
                break;
        }
        this.EditDataBind();
       
    }		

}
